package com.yzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzx.constants.Constants;
import com.yzx.domain.SysDictType;
import com.yzx.vo.DataGridView;
import dto.DictDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzx.domain.SysDictData;
import com.yzx.mapper.SysDictDataMapper;
import com.yzx.service.SysDictDataService;
@Service
public class SysDictDataServiceImpl implements SysDictDataService{

    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public DataGridView listPage(DictDataDto dictDataDto) {
        QueryWrapper<SysDictData> qw=new QueryWrapper<>();
        Page<SysDictData> page=new Page<>(dictDataDto.getPageNum(),dictDataDto.getPageSize());
        qw.eq(StringUtils.isNotBlank(dictDataDto.getDictType()), SysDictData.COL_DICT_TYPE,dictDataDto.getDictType());
        qw.like(StringUtils.isNotBlank(dictDataDto.getDictLabel()), SysDictData.COL_DICT_LABEL,dictDataDto.getDictLabel());
        qw.eq(StringUtils.isNotBlank(dictDataDto.getStatus()), SysDictData.COL_STATUS,dictDataDto.getStatus());
        this.dictDataMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public int insert(DictDataDto dictDataDto) {
        SysDictData dictData=new SysDictData();
        BeanUtil.copyProperties(dictDataDto,dictData);
        //设置创建者，创建时间
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        dictData.setCreateTime(DateUtil.date());
        return this.dictDataMapper.insert(dictData);
    }

    @Override
    public int update(DictDataDto dictDataDto) {
        SysDictData dictData=new SysDictData();
        BeanUtil.copyProperties(dictDataDto,dictData);
        //设置修改人
        dictData.setUpdateBy(dictDataDto.getSimpleUser().getUserName());
        Wrapper<SysDictData> wrapper=new QueryWrapper<>();
        ((QueryWrapper<SysDictData>) wrapper).eq(SysDictData.COL_DICT_CODE,dictData.getDictCode());
        return dictDataMapper.update(dictData,wrapper);
    }

    @Override
    public int deleteDictDataByIds(Long[] dictCodeIds) {
        List<Long> ids= Arrays.asList(dictCodeIds);
        if(null!=ids&&ids.size()>0){
            return this.dictDataMapper.deleteBatchIds(ids);
        }else{
            return -1;
        }
    }
    @Override
    public List<SysDictData> selectDictDataByDictType(String dictType) {
        ValueOperations<String,String> operations=redisTemplate.opsForValue();
        String json = operations.get(Constants.DICT_REDIS_PROFIX + dictType);
        List<SysDictData> sysDictData = JSON.parseArray(json, SysDictData.class);
        return sysDictData;
    }

    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return this.dictDataMapper.selectById(dictCode);
    }

    @Override
    public List<SysDictType> selectAllDictType() {
        return null;
    }
}
